import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// types
const types = {
  SET_LOCATION: 'SET_LOCATION',
  SET_ADDRESS: 'SET_ADDRESS',
  SET_FOODSELETE: 'SET_FOODSELETE',
  SET_USERINFO: 'SET_USERINFO',
  REMARKINFO: 'REMARK_INFO'
}
// state
const state = {
  location: {},
  address: '',
  foodSelect: null,
  userInfo: {},
  // 餐具
  remarkInfo: {
    tableware: '',
    remark: ''
  }
}

// getters
const getters = {
  location: state => state.location,
  address: state => state.address,
  foodSelect: state => state.foodSelect,
  userInfo: state => state.userInfo,
  totalPrice: state => {
    let price = 0
    if (state.foodSelect) {
      const selectFoods = state.foodSelect.selectFoods
      selectFoods.forEach(item => {
        price += (item.count * item.activity.fixed_price)
      })
      price += state.foodSelect.shopInfo.float_delivery_fee
    }
    return price
  },
  remarkInfo: state => state.remarkInfo
}

// mutations
const mutations = {
  [types.SET_LOCATION](state, location) {
    if (location) {
      state.location = location
    } else {
      state.location = null
    }
  },
  [types.SET_ADDRESS](state, address) {
    if (address) {
      state.address = address
    } else {
      state.address = ''
    }
  },
  [types.SET_FOODSELETE](state, foodSelect) {
    if (foodSelect) {
      state.foodSelect = foodSelect
    } else {
      state.foodSelect = ''
    }
  },
  [types.SET_USERINFO](state, userInfo) {
    if (userInfo) {
      state.userInfo = userInfo
    } else {
      state.userInfo = ''
    }
  },
  [types.REMARKINFO](state, remarkInfo) {
    if (remarkInfo) {
      state.remarkInfo = remarkInfo
    } else {
      state.remarkInfo = ''
    }
  }
}

// actions
const actions = {
  setLocation: ({ commit }, location) => {
    commit(types.SET_LOCATION, location)
  },
  setAddress: ({ commit }, address) => {
    commit(types.SET_ADDRESS, address)
  },
  setFoodSelect: ({ commit }, foodSelete) => {
    commit(types.SET_FOODSELETE, foodSelete)
  },
  setUserInfo: ({ commit }, userInfo) => {
    commit(types.SET_USERINFO, userInfo)
  },
  setRemarkInfo: ({ commit }, remarkInfo) => {
    commit(types.REMARKINFO, remarkInfo)
  }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
